<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Clojure documentation browser</title><style>
.library
{
  padding: 0.5em 0 0 0 
}
.all-libs-toggle,.library-contents-toggle
{
 font-size: small;
}
.all-libs-toggle a,.library-contents-toggle a
{
 color: white
}
.library-member-doc-whitespace
{
 white-space: pre
}
.library-member-source-toggle
{
  font-size: small;
  margin-top: 0.5em
}
.library-member-source
{
  display: none;
  border-left: solid lightblue 
}
.library-member-docs
{
  font-family:monospace
}
.library-member-arglists
{
  font-family: monospace
}
.library-member-type
{
  font-weight: bold; 
  font-size: small;
  font-style: italic;
  color: darkred
}
.lib-links
{
  margin: 0 0 1em 0
}

.lib-link-header
{
  color: white;
  background: darkgreen;
  width: 100%
}

.library-name 
{ 
  color: white;
  background: darkblue;
  width: 100%
}

.missing-library
{
  color: darkred; 
  margin: 0 0 1em 0 
}

.library-members
{
  list-style: none
}

.library-member-name
{
  font-weight: bold;
  font-size: 105%
}</style><script language="JavaScript" type="text/javascript"> // <![CDATA[

function getElem(id)
{
  if( document.getElementById )
  {
    return document.getElementById( id )
  }
  else if ( document.all )
  {
    return eval( 'document.all.' + id )
  }
  else
    return false;
}

function setDisplayStyle(id,displayStyle)
{
  var elem = getElem (id)
  if (elem)
  {
    elem.style.display = displayStyle
  }

}

function setLinkToggleText (id, text)
{
 var elem = getElem (id)
 if (elem)
 {
   elem.innerHTML = text
 }
}

function collapse(id)
{
  setDisplayStyle (id, 'none')
}

function expand (id)
{
  setDisplayStyle (id, 'block')
}

function toggleSource( id )
{
  toggle(id, 'linkto-' + id, 'Hide Source', 'Show Source')
}

function toggle(targetid, linkid, textWhenOpen, textWhenClosed)
{
  var elem = getElem (targetid)
  var link = getElem (linkid)

  if (elem && link)
  {
    var isOpen = false
    if (elem.style.display == '')
    {
      isOpen = link.innerHTML == textWhenOpen
    }
    else if( elem.style.display == 'block' )
    {
      isOpen = true
    }
    
    if (isOpen)
    {
      elem.style.display = 'none'
      link.innerHTML = textWhenClosed
    }
    else
    {
      elem.style.display = 'block'
      link.innerHTML = textWhenOpen
    }
  }
}

      //]]>
</script><script language="JavaScript" type="text/javascript">// <![CDATA[!
function expandAllNamespaces()
{
expand(&apos;library-contents-read.readDB&apos;);
setLinkToggleText(&apos;library-contents-toggle-read.readDB&apos;, &apos;-&apos;);

}function collapseAllNamespaces()
{
collapse(&apos;library-contents-read.readDB&apos;);
setLinkToggleText(&apos;library-contents-toggle-read.readDB&apos;, &apos;+&apos;);

}
// ]]></script></head><body><div class="lib-links"><div class="lib-link-header">Namespaces<span class="all-libs-toggle"> [ <a href="javascript:expandAllNamespaces()">Expand All</a> ] [ <a href="javascript:collapseAllNamespaces()">Collapse All</a> ]</span></div><a class="lib-link" href="#library-read.readDB">read.readDB</a></div><div class="library"><a name="library-read.readDB"/><div class="library-name"><span class="library-contents-toggle">[ <a id="library-contents-toggle-read.readDB" href="javascript:toggle(&apos;library-contents-read.readDB&apos;, &apos;library-contents-toggle-read.readDB&apos;, &apos;-&apos;, &apos;+&apos;)">-</a> ] </span>read.readDB</div><div class="library-contents" id="library-contents-read.readDB"><div class="library-member-links"><a class="lib-member-link" href="#member-read.readDB-byte-array">byte-array</a> <a class="lib-member-link" href="#member-read.readDB-erase-record">erase-record</a> <a class="lib-member-link" href="#member-read.readDB-fill">fill</a> <a class="lib-member-link" href="#member-read.readDB-read-bin-file">read-bin-file</a> <a class="lib-member-link" href="#member-read.readDB-read-fields-meta">read-fields-meta</a> <a class="lib-member-link" href="#member-read.readDB-read-one-record">read-one-record</a> <a class="lib-member-link" href="#member-read.readDB-read-records">read-records</a> <a class="lib-member-link" href="#member-read.readDB-read-str-len">read-str-len</a> <a class="lib-member-link" href="#member-read.readDB-write-empty-row">write-empty-row</a> <a class="lib-member-link" href="#member-read.readDB-write-row-with-values">write-row-with-values</a></div><ol class="library-members"><li class="library-member"><a name="member-read.readDB-byte-array"/><dl class="library-member-table"><dt class="library-member-name">byte-array</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([n])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Return an array of byte primitives of size n.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-byte-array&apos;)" id="linkto-membersource-read.readDB-byte-array">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-byte-array"><pre>;;; Uncomment this function if using Clojure 1.1 Alpha
(defn byte-array 
  &quot;Return an array of byte primitives of size n.&quot;
  [n]
  (make-array Byte/TYPE n))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-erase-record"/><dl class="library-member-table"><dt class="library-member-name">erase-record</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([fileName numOfRow])</span></div><div class="library-member-docs"/></dd></dl></li><li class="library-member"><a name="member-read.readDB-fill"/><dl class="library-member-table"><dt class="library-member-name">fill</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([buffer string])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Fills the buffer with new values</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-fill&apos;)" id="linkto-membersource-read.readDB-fill">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-fill"><pre>
(defn fill 
&quot;Fills the buffer with new values. 
 Receives two parameters: the value in bufer, the name where value will be located&quot; 
  [buffer string]
  (.put buffer (.getBytes string &quot;US-ASCII&quot;)))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-read-bin-file"/><dl class="library-member-table"><dt class="library-member-name">read-bin-file</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([file-name])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Reads a binary file and returns a vector with its information.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-read-bin-file&apos;)" id="linkto-membersource-read.readDB-read-bin-file">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-read-bin-file"><pre>;-------------------------------------------------------------------------------
(defn read-bin-file
  &quot;Reads a binary file and returns a vector with its information.&quot;
  [file-name]
  (with-open [file (DataInputStream. (FileInputStream. file-name))]
    (let [magic-number (.readInt file)
          offset       (.readInt file)
          num-fields   (.readShort file)
          fields-meta  (read-fields-meta file num-fields)
          records      (read-records file fields-meta)
		  ]
      {:magic magic-number :offset offset :num-fields num-fields 
       :fields fields-meta :records records})))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-read-fields-meta"/><dl class="library-member-table"><dt class="library-member-name">read-fields-meta</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([file num-fields])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Reads the meta-data of all the fields from the given file. You must specify </div><div class="library-member-doc-line">  the total number of fields.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-read-fields-meta&apos;)" id="linkto-membersource-read.readDB-read-fields-meta">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-read-fields-meta"><pre>;-------------------------------------------------------------------------------
(defn read-fields-meta
  &quot;Reads the meta-data of all the fields from the given file. You must specify ...&quot;
  [file num-fields]
  (loop [n num-fields
         resultado []]
    (if (= n 0)
        resultado
        (let [name-len   (.readShort file)
              field-name (read-str-len file name-len)
              field-len  (.readShort file)]
             (recur (dec n) (conj resultado [field-name field-len]))))))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-read-one-record"/><dl class="library-member-table"><dt class="library-member-name">read-one-record</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([file fields])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Reads one individual record from the given file. You must specify a vector </div><div class="library-member-doc-line">  with the fields&apos; meta-data.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-read-one-record&apos;)" id="linkto-membersource-read.readDB-read-one-record">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-read-one-record"><pre>;-------------------------------------------------------------------------------
(defn read-one-record
  &quot;Reads one individual record from the given file. You must specify a vector ...&quot;
  [file fields]
  (loop 
    [result {:deleted (not (zero? (.readShort file)))}
     [[fname fsize] &amp; rest-fields] fields]
     (if fname
         (recur (assoc result (keyword fname) (.trim (read-str-len file fsize)))
                 rest-fields)
         result)))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-read-records"/><dl class="library-member-table"><dt class="library-member-name">read-records</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([file fields])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Reads all the individual records of the file.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-read-records&apos;)" id="linkto-membersource-read.readDB-read-records">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-read-records"><pre>;-------------------------------------------------------------------------------
(defn read-records
  &quot;Reads all the individual records of the file.&quot;
  [file fields]
  (loop 
    [result []]
    (if (zero? (.available file))
     
		result
        (recur (conj result (read-one-record file fields))))))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-read-str-len"/><dl class="library-member-table"><dt class="library-member-name">read-str-len</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([file n])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Read the next n bytes from file and create a string.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-read-str-len&apos;)" id="linkto-membersource-read.readDB-read-str-len">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-read-str-len"><pre>
(defn read-str-len
  &quot;Read the next n bytes from file and create a string.&quot;
  [file n]
  (let [barray (byte-array n)]
    (.readFully file barray)
    (String. barray)))</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-write-empty-row"/><dl class="library-member-table"><dt class="library-member-name">write-empty-row</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([name numOfRecords])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Writes an empty row at the end of the total records.</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-write-empty-row&apos;)" id="linkto-membersource-read.readDB-write-empty-row">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-write-empty-row"><pre>;-------------------------------------------------------------------------------
(defn write-empty-row
  &quot;Writes an empty row at the end of the total records.&quot;
  [name numOfRecords]
  (let[
		fileToRead (File. &quot;db-1x2.db&quot;)
		fileRA	(RandomAccessFile. fileToRead &quot;rw&quot;)
		fileChannel (.getChannel fileRA)
		buffer (ByteBuffer/allocate 161)
		bufferCopy (ByteBuffer/allocate 161)
		magic-number (.readInt fileRA)
        offset       (.readInt fileRA)
        num-fields   (.readShort fileRA)
        fields-meta  (read-fields-meta fileRA num-fields)
	  ]
	
	(.position fileChannel (+ offset (* 161 numOfRecords)))
	(println (str &quot;FileChannel before allocation length: &quot; (.size fileChannel)))
	
	(.write fileChannel buffer)
	;(println (str &quot;Number of bytes read: &quot; (.read fileChannel bufferCopy)))
	(println (str &quot;FileChannel after insertion length: &quot; (.size fileChannel)))
	
	(.close fileChannel)
	(.close fileRA)
  )
)</pre></div></div></dd></dl></li><li class="library-member"><a name="member-read.readDB-write-row-with-values"/><dl class="library-member-table"><dt class="library-member-name">write-row-with-values</dt><dd><div class="library-member-info"><span class="library-member-type">fn</span> <span class="library-member-arglists">([fileName numOfRow name location size smoking rate date owner])</span></div><div class="library-member-docs"><div class="library-member-doc-line">Write in the database the new values</div></div><div class="library-member-source-section"><div class="library-member-source-toggle">[ <a href="javascript:toggleSource(&apos;membersource-read.readDB-write-row-with-values&apos;)" id="linkto-membersource-read.readDB-write-row-with-values">Show Source</a> ]</div><div class="library-member-source" id="membersource-read.readDB-write-row-with-values"><pre>  (.put buffer (.getBytes string &quot;US-ASCII&quot;))</pre></div></div></dd></dl></li></ol></div></div></body></html>